<?php
/**
 * Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
OCP\JSON::checkLoggedIn();
OCP\JSON::checkAppEnabled('calendar');
OCP\JSON::callCheck();

foreach ($_POST as $key=>$element) {
   debug('_POST: '.$key.'=>'.print_r($element, true));
}

function bailOut($msg) {
   OCP\JSON::error(array('data' => array('message' => $msg)));
   OCP\Util::writeLog('calendar','ajax/categories/rescan.php: '.$msg, OCP\Util::DEBUG);
   exit();
}
function debug($msg) {
   OCP\Util::writeLog('calendar','ajax/categories/rescan.php: '.$msg, OCP\Util::DEBUG);
}
$calendar_model = new PCalendarModelCalendar();
$app_model = new PCalendarModelApp();

$calendars = $calendar_model->allCalendars(OC_USER::getUser());
if(count($calendars) == 0) {
   bailOut('没有找到该日历！');
}
$events = array();
$object_model = new PCalendarModelObject();
foreach($calendars as $calendar) {
   $calendar_events = $object_model->all($calendar['id']);
   $events = $events + $calendar_events;
}
if(count($events) == 0) {
   bailOut('没有找到该日程！');
}

$app_model->scanCategories($events);
$categories = $app_model->getCategoryOptions();

OCP\JSON::success(array('data' => array('categories'=>$categories)));